package com.tepusoft.weixin.controller;

import com.tepusoft.weixin.model.User;
import com.tepusoft.weixin.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.List;

/**
 * User: GIE
 * Date: 2015/12/14
 * description：
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private SessionDAO sessionDAO;

	@RequestMapping("add")
	public String add(User user) {
		userService.saveUser(user);
		return "redirect:list";
	}

	@RequestMapping("list")
	public String list(Model model) {
		List<User> list = userService.findAll();
		model.addAttribute("list", list);
		return "userList";
	}

	@ResponseBody
	@RequestMapping("index")
	public String indesA() {
		return "index";
	}

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String loginUI() {
		return "login";
	}

	@ResponseBody
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String doLogin() {
		Subject subject = SecurityUtils.getSubject();
		subject.login(new UsernamePasswordToken("admin", "123"));
		return "SUCCESS";
	}

	@RequestMapping("listSession")
	public String listSession(Model model){
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		model.addAttribute("sessions", sessions);
		model.addAttribute("sesessionCount", sessions.size());
		return "listsession";
	}

}
